package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.aps.AdoWorkDetail;
import com.zmj.sy.mom.srv.aps.bean.vo.apsworkdetail.TomorrowScheduleListVo;
import com.zmj.sy.mom.srv.aps.bean.vo.dayworkorder.ApsDayOrderAdoListResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AdoWorkDetailMapper extends SyMapper<AdoWorkDetail>{

    @Select(" SELECT " +
            "  o.id AS orderId, " +
            "  o.construct_no AS construNum, " +
            "  ob.work_order_code AS nxolsection,  " +
            "  ob.plm_id AS partNo,  " +
            "  awd.mater_name AS partName,  " +
            "  ob.standards AS dimension,  " +
            "  awd.order_count AS planJobCount,  " +
            "  awd.plan_start_date_time AS planStartDate,  " +
            "  awd.plan_end_date_time AS dueDate,  " +
            "  awd.phase_code AS nextWorkPlace,  " +
            "  op.png_url AS palletingDrawingUrl,  " +
            "  s.my_code AS workPlaceName   " +
            " FROM " +
            "  tb_ado_work_detail awd  " +
            "  LEFT JOIN tb_order o ON awd.order_id = o.id and o.deleted = 0 " +
            "  LEFT JOIN tb_order_phase ph ON awd.phase_id = ph.id  and ph.deleted = 0 " +
            "  LEFT JOIN tb_order_bom ob ON ph.bom_id = ob.id and ob.deleted = 0 " +
            "  LEFT JOIN tb_station s ON awd.station_id = s.id and s.deleted = 0 " +
            "  LEFT JOIN tb_order_part op ON awd.plm_id = op.plm_id and op.deleted = 0  " +
            " WHERE awd.group_code = #{workPlaceName} " +
            "  and awd.plan_start_date_time >= #{startDate} " +
            "  and awd.plan_start_date_time < #{endDate} " +
            " and awd.deleted = 0 " +
            " order by awd.plan_start_date_time ")
    List<TomorrowScheduleListVo> findAdoTomorrowSchedule(@Param("workPlaceName") String workPlaceName, @Param("startDate") String startDate, @Param("endDate") String endDate);

    @Select(" SELECT awd.id id, awd.phase_id phaseId, awd.bom_id bomId, o.construct_no constructionCode, o.mater_name deptName, ob.mater_name parentName, ob.plm_id parentPlmId, awd.plm_id plmId, awd.mater_name materName, awd.phase_code phaseCode, awd.group_code groupCode, awd.order_count planCount, awd.plan_start_date_time  planStartDateTime, awd.modify_username modifyUsername " +
            " FROM tb_ado_work_detail awd " +
            " LEFT JOIN tb_order_bom ob on awd.bom_pid = ob.id and ob.deleted = 0" +
            " LEFT JOIN tb_order o on awd.order_id = o.id and o.deleted = 0" +
            " ${ew.customSqlSegment}")
    Page<ApsDayOrderAdoListResVo> findList(@Param(Constants.WRAPPER) QueryWrapper<AdoWorkDetail> wp, Page<Object> page);

    @Select(" select IFNULL(sum(order_count), 0) from tb_ado_work_detail awd where awd.phase_id = #{phaseId} and awd.deleted = 0 ")
    Long getPlanTotal(@Param("phaseId") Integer phaseId);
}
